<template>
  <c-dialog ref="dialog" :data="selData" single :warn-text="title" :title="title">
    <c-list ref="form" :model="formData" :rules="formRules" label-width="120px">
      <c-list-item label="软装陈列任务号" :value="detail.softwearTaskCode" />
      <c-list-item label="所属门店" :value="detail.shopName" />
      <c-list-item label="装修任务号" :value="detail.decorationTaskCode" />
      <c-list-item prop="initInspectionTime" :label="`软装${prefix}完成时间`">
        <el-date-picker
          v-model="formData.initInspectionTime"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期"
          clearable
        />
      </c-list-item>
      <c-list-item :label="`${prefix}结果`" prop="initInspectionResult">
        <el-select v-model="formData.initInspectionResult" :placeholder="`${prefix}结果`" clearable>
          <el-option label="通过" :value="1" />
          <el-option label="不通过" :value="0" />
        </el-select>
      </c-list-item>
      <c-list-item :label="`${prefix}结果说明`" prop="initInspectionResultDesc">
        <el-input v-model="formData.initInspectionResultDesc" maxlength="1024" show-word-limit type="textarea" :autosize="{minRows: 3}" :placeholder="`${prefix}结果说明`" clearable />
      </c-list-item>
      <c-list-item :label="`${prefix}附件结果上传`">
        <c-file-list v-model="formData.files" :type-code="recheck?'ACCESS026':'ACCESS025'" view-type="image" />
      </c-list-item>
      <c-list-item>
        <div class="btn-container">
          <c-button @click="cancelHandle">取消</c-button>
          <c-button type="primary" @click="confirmHandle">确认</c-button>
        </div>
      </c-list-item>
    </c-list>
  </c-dialog>
</template>
<script>
import { deepClone } from '@/utils'
import { postFirstCheck, postReCheck } from '@/api/softDecorate'
export default {
  name: 'Check',
  components: {
  },
  props: {
    selData: {
      type: Array,
      default: () => []
    },
    recheck: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const prefix = this.recheck ? '复检' : '初检'
    return {
      formData: {
        initInspectionResult: '',
        initInspectionResultDesc: '',
        initInspectionTime: '',
        files: []
      },
      formRules: {
        initInspectionResult: [{ required: true, message: `请选择${prefix}结果`, trigger: 'change' }],
        initInspectionResultDesc: [{ required: true, message: `请输入${prefix}结果说明`, trigger: 'change' }],
        initInspectionTime: [{ required: true, message: `请选择软装${prefix}完成时间`, trigger: 'change' }],
        files: []
      }
    }
  },
  computed: {
    detail() {
      return this.selData.length > 0 ? this.selData[0] : {}
    },
    prefix() {
      return this.recheck ? '复检' : '初检'
    },
    title() {
      return `软装${this.prefix}`
    }
  },
  methods: {
    confirmHandle() {
      this.$refs.form.validate().then(() => {
        // TODO: 请求
        let data = {}
        let ajax = postFirstCheck
        this.formData.files = this.$getFiles(this.formData.files, this.detail.id)
        if (!this.recheck) {
          data = deepClone(this.formData)
        } else {
          ajax = postReCheck
          data.reinspectionTime = this.formData.initInspectionTime
          data.reinspectionResultDesc = this.formData.initInspectionResultDesc
          data.reinspectionResult = this.formData.initInspectionResult
          data.files = this.formData.files
        }
        data.id = this.detail.id
        data.softwearTaskCode = this.detail.softwearTaskCode
        ajax(data).then(res => {
          this.$message.success('操作成功！')
          this.$emit('on-success')
          this.$refs.dialog.visible = false
        })
      })
    },
    cancelHandle() {
      this.$refs.dialog.visible = false
    }
  }
}
</script>
